package com.nf.service;

import com.nf.dao.AccountMapper;
import com.nf.dao.RecordMapper;
import com.nf.entity.Account;
import com.nf.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private RecordMapper recordMapper;

    //登入
    @Transactional
    public Account login(String userName, String password) {
        Account account = accountMapper.getAccountByUserName(userName);
        if (account==null){
            return null;
        }else if(  password.equals( account.getPassword() )  ){
            return account;
        }else {
            return null;
        }
    }
    //查询余额
    @Transactional
    public Integer queryBanlanceById(Integer userid) {
        Account account = accountMapper.getAccountById(userid);
        if (account==null){
            return null;
        }else{
            return account.getBalance();
        }

    }
    //修改密码
    @Transactional
    public boolean updatePassword(String newPassword, String oldPassword, Integer userId) {
        Account account = accountMapper.getAccountById(userId);
        if (account==null){
            return false;
        }
        if (  oldPassword.equals( account.getPassword() )&&(!oldPassword.equals(newPassword))  ){
            //做修改密码操作
            account.setPassword(newPassword);
            accountMapper.updateAccount(account);
            return true;
        }else{
            //和旧密码不一致
            return false;
        }

    }
    //存钱
    //1.判断user是否存在
    //2.判断账号是否过了有效期
    //3.判断是否大于1万
    //4.添加一条流水记录（Record,类型是存钱）
    //5.修改余额 自增
    public boolean saveMoney(Integer money, Integer userId) {
        Account account = accountMapper.getAccountById(userId);
        if(account==null){
            return false;//这个方法以及结束了
        }
        //2.判断账号是否过了有效期
        if((System.currentTimeMillis()-account.getValidDate().getTime())>0){
            return false;
        }
        //3.判断是否大于1万
        if(money>10000||money<=0){
            return false;
        }
        //4.添加一条流水记录
        Record record=new Record();
        System.out.println("插入之前record id:"+record.getId());
        record.setUserId(userId);
        record.setM(money);
        record.setType(1);
        record.setDay(new Date());
        record.setDes("人民币存款");
        System.out.println("插入之后record id:"+record.getId());
        recordMapper.addRecord(record);
        //5.修改余额 自增
        account.setBalance(money+account.getBalance());
        accountMapper.updateAccount(account);//由面向过程的思维，升级到面向对象的思维，修改account这个账号对象的状态（自然包含了这个字段）


        return true;
    }
    //取钱
    //1.检查userId的账号account是否存在
    //2.判断有效期
    //3.判断余额是否足够
    //4.1次只能取2000元
    //5.每天只能取5次
    //6.每天上限8000元
    //7.每次手续费1%上限5元
    //8.插入2条流水记录（本金，手续费）
    //9.余额减少
    public boolean getMoney(Integer money, Integer userId) {
        Account account = accountMapper.getAccountById(userId);
        //1.检查userId的账号account是否存在
        if(account==null){
            return false;
        }
        //2.判断有效期
        if((System.currentTimeMillis()-account.getValidDate().getTime())>0){
            return false;
        }
        //3.判断余额是否足够
        if(account.getBalance()<money){
            return false;
        }
        //4.1次最多只能取2000元
        if(money>2000){
            return false;
        }
        //5.每天只能取5次
        List<Record> recordList = recordMapper.todayRecord(userId, new java.sql.Date(System.currentTimeMillis()));
        if(recordList.size()>=5){
            return false;
        }
        //6.每天上限8000元
        int getmoneyCount=0;
        for(Record r:recordList){
            getmoneyCount=getmoneyCount+r.getM();
        }
        if(getmoneyCount>=8000){
            return false;
        }
        //7.每次手续费1%上限5元
        int poundage=money/100;
        if(poundage>5){
            poundage=5;
        }
        //8.插入2条流水记录（本金，手续费）
        Record record=new Record();
        record.setUserId(userId);
        record.setM(money);
        record.setType(2);
        record.setDes("取钱的本金"+money);
        record.setDay(new java.util.Date());
        recordMapper.addRecord(record);

        Record record2=new Record();
        record2.setUserId(userId);
        record2.setM(poundage);
        record2.setType(5);
        record2.setDes("取钱的手续费"+poundage);
        record2.setDay(new java.util.Date());
        recordMapper.addRecord(record2);

        //9.余额减少
        int balance_new=account.getBalance()-money-poundage;
        account.setBalance(balance_new);
        accountMapper.updateAccount(account);

        return true;
    }
    //1.判断当前用户是否存在
    //2.判断目标用户是否存在
    //3.判断当前用户是否超过有效期
    //4.判断目标用户是否超过有效期
    //5.先算手续费
    //6.当前用户余额是否足够（本金+手续费）
    //7.一次最多转1000000元
    //8.当天最多2000000元（没有次数限制）
    //9.插入转出本金记录
    //10.插入转出手续费记录
    //11.插入转入记录
    //12.修改当前用户的余额
    //13.修改目标用户的余额
    public boolean transMoney(Integer money, Integer userIdDest, Integer userIdMyself) {
        Account account = accountMapper.getAccountById(userIdMyself);
        Account accoundest = accountMapper.getAccountById(userIdDest);
        //1.检查当前是否存在
        if(account==null){
            return false;
        }
        //2.判断目标用户是否存在
        if(accoundest==null){
            return false;
        }
        //3.判断当前用户是否超过有效期
        if((System.currentTimeMillis()-account.getValidDate().getTime())>0){
            return false;
        }
        //4.判断目标用户是否超过有效期
        if((System.currentTimeMillis()-accoundest.getValidDate().getTime())>0){
            return false;
        }
        //5.每次手续费1%上限5元
        int poundage=money/100;
        if(poundage>5){
            poundage=5;
        }
        //6.当前用户余额是否足够（本金+手续费）
        if(account.getBalance()<(money+poundage)){
            System.out.println("余额不足");
            return false;
        }
        //7.一次最多转1000000元
        if(money>1000000){
            return  false;
        }
        //8.当天最多2000元（没有次数限制）
        //8.1取出所有的记录
        //8.2遍历所有的记录,统计一下一共转了多少钱
        List<Record> recordList = recordMapper.queryTransOutRecord(userIdMyself, new java.sql.Date(System.currentTimeMillis()));
        int moneyOut=0;
        for(Record r:recordList){
            moneyOut+=r.getM();
        }
        if(moneyOut>10000000){
            return  false;
        }
        //9.插入转出记录
        Record recordOut=new Record();
        recordOut.setUserId(userIdMyself);
        recordOut.setM(money);
        recordOut.setDay(new java.util.Date());
        recordOut.setType(4);
        recordOut.setDes("转出{本金}");
        recordMapper.addRecord(recordOut);

        //10.插入转出手续费记录
        Record recordOut_poundage=new Record();
        recordOut_poundage.setUserId(userIdMyself);
        recordOut_poundage.setM(poundage);
        recordOut_poundage.setDay(new java.util.Date());
        recordOut_poundage.setType(5);
        recordOut_poundage.setDes("转出{手续费}");
       recordMapper.addRecord(recordOut_poundage);
        //11.插入转入记录
        Record recordIn=new Record();
        recordIn.setUserId(userIdDest);
        recordIn.setM(money);
        recordIn.setDay(new java.util.Date());
        recordIn.setType(3);
        recordIn.setDes("转入{来自："+userIdMyself+"("+account.getUserName()+")}");
        recordMapper.addRecord(recordIn);

        //12.修改当前用户的余额
        account.setBalance(account.getBalance()-(money+poundage));
        accountMapper.updateAccount(account);
        //13.修改目标用户的余额
        accoundest.setBalance(accoundest.getBalance()+money);
        accountMapper.updateAccount(accoundest);
        return true;
    }
}
